package com.mapforge.map.tools.tilewriting;

import java.awt.Point;

import com.mapforge.map.TileMap;
import com.mapforge.map.tilewriter.TileWriter;
import com.mapforge.map.tools.selection.EllipseSelectionTool;
import com.mapforge.selection.Selection;
import com.mapforge.selection.SelectionModifier;

/** */
public class EllipseTool extends AbstractShapeTileWriterTool {
	private EllipseSelectionTool ellipseTool;

	public EllipseTool() {
		super("Ellipse tool");
		this.ellipseTool = new EllipseSelectionTool();
	}

	public void setSelectionModifier(SelectionModifier modifier) {
		super.setSelectionModifier(modifier);
	}

	public void setTileMap(TileMap tilemap) {
		super.setTileMap(tilemap);
		this.ellipseTool.setTileMap(tilemap);
	}

	public void makSelection() {
		Point startPoint = getAnchorPoint();
		Point currentPoint = getMovingPoint();
		ellipseTool.setAnchorPoint(startPoint);
		ellipseTool.setMovingPoint(currentPoint);
		ellipseTool.makeSelection();
	}

	public Selection getCurrentSelection() {
		return ellipseTool.getCurrentSelection();
	}

	@Override
	public void beginEdit() {
		super.beginEdit();
		this.getTileWriter().beginPreview();
		this.makeSelection();
	}

	@Override
	public void performEdit() {
		this.makeSelection();
	}

	@Override
	public void endEdit() {
		super.endEdit();
		this.validateState();
		TileWriter tileWriter = this.getTileWriter();
		tileWriter.endPreview();
		this.makeSelection();
		tileWriter.beginWrite();
		tileWriter.writeTiles(getTileSelection(), getCurrentSelection());
		tileWriter.endWrite();
	}

	@Override
	public Selection performSelection(Point startPoint, Point currentPoint) {
		ellipseTool.setAnchorPoint(startPoint);
		ellipseTool.setMovingPoint(currentPoint);
		ellipseTool.setSelectionModifier(getSelectionModifier());
		ellipseTool.makeSelection();
		return ellipseTool.getCurrentSelection();
	}
}
